********************************************************************************
********************************************************************************
* Figure 3. REVENUES AND EXPENDITURES BY BUDGET GROUP
********************************************************************************
********************************************************************************

use "$data/groups.dta", clear
set more off

* DEFINE VARIABLES
global x 	migch_fd
global z	IV2005
global w 	totpop13_wa

* CREATE CONTROLS
*global msactrl feff sh_pgds sh_wap13 sh_college13 sh_manuf13
global msactrl c.feff#i.year c.sh_wap13#i.year c.sh_college13#i.year

* REVENUES
local varest inctx_d si_d wlthtx_d indtx_d proptx_d icatx_d 

matrix A = J(6,3,.)
local i=1
foreach l of local varest {
rename $x `l'_iv
qui reg `l' `l'_iv $msactrl i.year [aw = $w], robust
matrix A[`i',1] = _b[`l'_iv]
matrix A[`i',2] = _b[`l'_iv] - invttail(e(df_r),0.025)*_se[`l'_iv]
matrix A[`i',3] = _b[`l'_iv] + invttail(e(df_r),0.025)*_se[`l'_iv]
rename `l'_iv $x
local ++i 
}

matrix B = J(6,3,.)
matrix C = J(6,3,.)
local i=1
foreach l of local varest {
rename $x `l'_iv
qui ivreg2 `l' (`l'_iv = $z) $msactrl i.year [aw = $w], robust small
matrix B[`i',1] = _b[`l'_iv]
matrix B[`i',2] = _b[`l'_iv] - invttail(e(df_r),0.025)*_se[`l'_iv]
matrix B[`i',3] = _b[`l'_iv] + invttail(e(df_r),0.025)*_se[`l'_iv]

qui twostepweakiv 2sls `l' (`l'_iv = $z) $msactrl i.year [aw=$w], robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen `l'_ci = e(arci) 	// saves it as a varibale in data
split `l'_ci, parse(,)	// split at ,
replace `l'_ci1 = subinstr(`l'_ci1, "[", "", .) // removes [
replace `l'_ci2 = subinstr(`l'_ci2, "]", "", .) // removes ]
destring `l'_ci1 `l'_ci2, replace
sum `l'_ci1
return list
matrix C[`i',2] = r(mean)	// Low-CI
sum `l'_ci2
return list
matrix C[`i',3] = r(mean)	// High-CI
rename `l'_iv $x
drop `l'_ci `l'_ci1 `l'_ci2
local ++i 
}



matrix A=A'
matrix B=B'
matrix C=C'

coefplot (matrix(A[1]), ci((A[2] A[3])) label(OLS) ciopts(recast(rcap))) ///
(matrix(B[1]), ci((B[2] B[3])) m(D) msize(medsmall) label(2SLS) ciopts(recast(rcap))) ///
(matrix(B[1]), ci((C[2] C[3])) m(T) msize(medsmall) label(2SLS-AR) ciopts(recast(rcap)) ///
graphregion(color(white)) xtitle("Estimated effect", size(small)) ///
xlab(, labsize(small))), xline(0, lcolor(black) lwidth(thin) lpattern(dash)) ///
legend(rows(1)) ///
coeflabel(1 r1 = "Income tax" ///
		  2 r2 = "Social security" ///
		  3 r3 = "Wealth tax" ///
		  4 r4 = "VAT & indirect taxes" ///
		  5 r5 = "Property tax" ///
		  6 r6 = "Industry tax", labsize(small))

* EXPENDITURES
local varest ppgds_d cpgds_d water_d edexp_d hcserv_d sprot_d debtserv_d 

matrix D = J(7,3,.)
local i=1
foreach l of local varest {
rename $x `l'_iv
qui reg `l' `l'_iv $msactrl i.year [aw = $w], robust
matrix D[`i',1] = _b[`l'_iv]
matrix D[`i',2] = _b[`l'_iv] - invttail(e(df_r),0.025)*_se[`l'_iv]
matrix D[`i',3] = _b[`l'_iv] + invttail(e(df_r),0.025)*_se[`l'_iv]
rename `l'_iv $x
local ++i 
}

matrix E = J(7,3,.)
matrix F = J(7,3,.)
local i=1
foreach l of local varest {
rename $x `l'_iv
qui ivreg2 `l' (`l'_iv = $z) $msactrl i.year [aw = $w], robust small
matrix E[`i',1] = _b[`l'_iv]
matrix E[`i',2] = _b[`l'_iv] - invttail(e(df_r),0.025)*_se[`l'_iv]
matrix E[`i',3] = _b[`l'_iv] + invttail(e(df_r),0.025)*_se[`l'_iv]


qui twostepweakiv 2sls `l' (`l'_iv = $z) $msactrl i.year [aw=$w], robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen `l'_ci = e(arci) 	// saves it as a varibale in data
split `l'_ci, parse(,)	// split at ,
replace `l'_ci1 = subinstr(`l'_ci1, "[", "", .) // removes [
replace `l'_ci2 = subinstr(`l'_ci2, "]", "", .) // removes ]
destring `l'_ci1 `l'_ci2, replace
sum `l'_ci1
return list
matrix F[`i',2] = r(mean)	// Low-CI
sum `l'_ci2
return list
matrix F[`i',3] = r(mean)	// High-CI
rename `l'_iv $x
drop `l'_ci `l'_ci1 `l'_ci2
local ++i 
}



matrix D=D'
matrix E=E'
matrix F=F'

coefplot (matrix(D[1]), ci((D[2] D[3])) label(OLS) ciopts(recast(rcap))) ///
(matrix(E[1]), ci((E[2] E[3])) m(D) msize(medsmall) label(2SLS) ciopts(recast(rcap))) ///
(matrix(E[1]), ci((F[2] F[3])) m(T) msize(medsmall) label(2SLS-AR) ciopts(recast(rcap)) ///
graphregion(color(white)) xtitle("Estimated effect", size(small)) ///
xlab(, labsize(small))), xline(0, lcolor(black) lwidth(thin) lpattern(dash)) ///
legend(rows(1)) ///
coeflabel(1 r1 = "Pure public goods" ///
		  2 r2 = "Congested public goods" ///
		  3 r3 = "Water services" ///
		  4 r4 = "Education services" ///
		  5 r5 = "Health services" ///
		  6 r6 = "Social protection" ///
		  9 r7 = "Debt service", labsize(small))

		  
